package com.example.zwlyz;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.zwlyz.models.Product;

public class AddProductActivity extends AppCompatActivity {

    private EditText etName;
    private EditText etPrice;
    private EditText etDescription;
    private Button btnAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_product);

        etName = findViewById(R.id.etName);
        etPrice = findViewById(R.id.etPrice);
        etDescription = findViewById(R.id.etDescription);
        btnAdd = findViewById(R.id.btnAdd);

        btnAdd.setOnClickListener(v -> {
            String name = etName.getText().toString();
            String priceStr = etPrice.getText().toString();
            String description = etDescription.getText().toString();

            if (!name.isEmpty() && !priceStr.isEmpty() && !description.isEmpty()) {
                try {
                    double price = Double.parseDouble(priceStr);
                    int id = ProductManager.getAllProducts().size() + 1;
                    Product product = new Product(id, name, price, "default_image_url", description);
                    ProductManager.addProduct(product);
                    Toast.makeText(this, "商品添加成功", Toast.LENGTH_SHORT).show();
                    finish();
                } catch (NumberFormatException e) {
                    Toast.makeText(this, "请输入有效的价格", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(this, "请填写完整信息", Toast.LENGTH_SHORT).show();
            }
        });
    }
}